package com.lindroy.androidplayer.ui.knowledge

import android.os.Bundle
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseFragment
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.databinding.FragmentKnowledgeDetailBinding
import com.lindroy.androidplayer.logic.model.bean.TabBean
import com.lindroy.androidplayer.ui.adapter.KnowledgeTabAdapter
import com.lindroy.androidplayer.utils.contactToViewPager
import com.lindroy.androidplayer.utils.no
import com.lindroy.androidplayer.utils.orZero
import com.lindroy.androidplayer.utils.yes

/**
 * @author Lin
 * @date 2022/1/24
 * @function 体系详情页面
 * @Description
 */
class KnowledgeDetailFragment : BaseLazyFragment<FragmentKnowledgeDetailBinding>() {

    private val itemList by lazy { arguments?.getSerializable(KEY_KNOWLEDGE_TABS) as List<TabBean> }

    private val chapterName by lazy { itemList[0].parentName }

    override fun FragmentKnowledgeDetailBinding.initView(savedInstanceState: Bundle?) {
        initToolbar(chapterName.isNotEmpty().yes { chapterName }
            .no { getString(R.string.knowledge_system) })
        vpKnowledge.adapter =  KnowledgeTabAdapter(this@KnowledgeDetailFragment).apply { setData(itemList.map { it.id }) }
        vpKnowledge.offscreenPageLimit = itemList.size
        tab.tabLayout.contactToViewPager(vpKnowledge, itemList.map { it.name })
        vpKnowledge.setCurrentItem(itemList.indexOfFirst { it.isSelected }.takeIf { it >= 0 }.orZero(),false)
    }

    override fun FragmentKnowledgeDetailBinding.initListener() {
    }

    override fun lazyLoad() {
    }

    companion object {
        private const val KEY_KNOWLEDGE_TABS = "key_knowledge_tabs"
        fun navigateTo(
            fragment: BaseFragment<*>,
            items: ArrayList<TabBean>,
        ) {
            fragment.activityNavController.navigate(R.id.action_to_knowledgeDetail, Bundle().apply {
                putSerializable(KEY_KNOWLEDGE_TABS, items)
            })
        }
    }
}